package com.laowang.controller;

import com.laowang.dataoject.ProductCategory;
import com.laowang.dataoject.ProductInfo;
import com.laowang.form.ProductForm;
import com.laowang.sercice.CategoryService;
import com.laowang.sercice.ProductService;
import com.laowang.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by Administrator on 2019/1/24.
 */
@Controller
@RequestMapping("/seller/product")
public class SellerProductController {
     @Autowired
     private ProductService productService;
     @Autowired
     private CategoryService categoryService;
    @GetMapping("/index")
     public ModelAndView index(@RequestParam(value = "productId",required = false)
                                              String productId, Map<String,Object> map){
         if(!StringUtils.isEmpty(productId)){
         Optional<ProductInfo> productInfo1 = productService.findById(productId);
         ProductInfo productInfo = productInfo1.get();
         map.put("productInfo",productInfo);
         }
         //选择类目
        List<ProductCategory> categoryList = categoryService.findAll();
        map.put("categoryList",categoryList);
         return new ModelAndView("product/index",map);
    }
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm form, BindingResult bindingResult,
                               Map<String,Object> map){
       if(bindingResult.hasErrors()){
           map.put("msg",bindingResult.getFieldError().getDefaultMessage());
           map.put("url","/sell/seller/product/index");
           return new ModelAndView("common/error",map);
       }
       ProductInfo productInfo = new ProductInfo();
       try {
           if(!StringUtils.isEmpty(form.getProductId())){
               Optional<ProductInfo> productInfo1 = productService.findById(form.getProductId());
               productInfo = productInfo1.get();
           }else {
               form.setProductId(KeyUtil.getUniqueKey());
           }
           BeanUtils.copyProperties(form,productInfo);
           productService.saveProduct(productInfo);
       }catch (Exception e){
           map.put("url","/sell/seller/product/index");
           map.put("msg",e.getMessage());
           return new ModelAndView("common/error",map);
       }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }

}
